package web.base;

import org.apache.commons.lang.StringUtils;
import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;
import utils.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户注册servlet
 * @author wyx
 * @version 1.0
 * @date 2020/11/9
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    UserService userService=new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        if (StringUtils.isEmpty(username)){
            request.setAttribute("msg","注册失败，请输入用户名");
            request.getRequestDispatcher("WEB-INF/views/main/register.jsp").forward(request,response);
        }
        String password = request.getParameter("password");
        if (StringUtils.isEmpty(password)){
            request.setAttribute("msg","注册失败，请输入密码");
            request.getRequestDispatcher("WEB-INF/views/main/register.jsp").forward(request,response);
        }
//        密码加密
        password = MD5Util.MD5Encode(password,"utf-8");

        String phone = request.getParameter("phone");

        int sex = 0;
        String sexStr = request.getParameter("sex");
        if(StringUtils.isNotEmpty(sexStr)){
            sex = Integer.parseInt(sexStr);
        }

        int age = 0;
        String ageStr = request.getParameter("age");
        if(StringUtils.isNotEmpty(ageStr)){
            age = Integer.parseInt(ageStr);
        }

        int roleId = 2;
        String roleIdStr = request.getParameter("roleId");
        if(StringUtils.isNotEmpty(roleIdStr)){
            roleId = Integer.parseInt(roleIdStr);
        }
//        判断用户是否存在
        User us=userService.queryUserByUsername(username);
        if(us!=null){
            request.setAttribute("msg","用户已存在");
            request.getRequestDispatcher("WEB-INF/views/main/register.jsp").forward(request,response);
        }else{
            //        进行用户注册
            User user=new User(null,username,password,phone,sex,age,roleId);

            int result = userService.registerUser(user);
            if (result>0){
                /**
                 * 注册成功返回信息
                 */
                request.setAttribute("msg","注册成功");
                request.getRequestDispatcher("WEB-INF/views/main/register.jsp").forward(request,response);
            }else {
                request.setAttribute("msg","注册失败");
                request.getRequestDispatcher("WEB-INF/views/main/register.jsp").forward(request,response);
            }
        }

    }
}
